﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows;
using Microsoft.Win32;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;

namespace Kernel
{
    public class Misc
    {
        /// <summary>
        /// CAD 2012以前版本中，向注册表中写入库自动加载配置，使酷可以自动加载
        /// </summary>
        /// <param name="dname"></param>
        /// <param name="desc"></param>
        /// <param name="dpath"></param>
        public static  void AutoLoad(string dname, string desc, string dpath)
        {
#if CAD_2008
            RegistryKey LocalMachine = Registry.LocalMachine;
            RegistryKey Applications = LocalMachine.OpenSubKey("SOFTWARE\\Autodesk\\AutoCAD\\R17.1\\ACAD 6001:804\\Aplications",true);
            RegistryKey MyProgram = Applications.CreateSubKey(dname);
#elif CAD_2014
            Autodesk.AutoCAD.Runtime.RegistryKey LocalMachine = Autodesk.AutoCAD.Runtime.Registry.LocalMachine;
            Autodesk.AutoCAD.Runtime.RegistryKey Applications = LocalMachine.OpenSubKey("SOFTWARE\\Autodesk\\AutoCAD\\R17.1\\ACAD 6001:804\\Aplications",true);
            Autodesk.AutoCAD.Runtime.RegistryKey MyProgram = Applications.CreateSubKey(dname);
#endif
            MyProgram.SetValue("DESCRIPTION",desc,RegistryValueKind.String);
            MyProgram.SetValue("LOADCTRLS", 14, RegistryValueKind.DWord);
            MyProgram.SetValue("LOADER", dpath, RegistryValueKind.String);
            MyProgram.SetValue("MANAGED", 1, RegistryValueKind.DWord);
        }

        /// <summary>
        /// 设置cad标题
        /// </summary>
        /// <param name="strTitle"></param>
        public static void ReNameTitle(string strTitle)
        {
            Application.MainWindow.Text = strTitle;
        }

        /// <summary>
        /// 设置cad图标  
        /// </summary>
        /// <param name="strIconPath"></param>
        public static void SetIcon(string strIconPath)
        {
#if CAD_2008
            Application.MainWindow.Icon = new Icon(strIconPath);
#elif CAD_2014
#endif
        }

        /// <summary>
        /// 设置cad图标
        /// </summary>
        /// <param name="ico"></param>
        public static void SetIcon(Icon ico)
        {
            //Document acDoc = Application.DocumentManager.MdiActiveDocument;
#if CAD_2008
            //acDoc.Window.WindowState = System.Windows.Forms.FormWindowState.Normal;
            Application.MainWindow.Icon = ico;
#elif CAD_2014
            Application.MainWindow.SetIcon(ico);
#endif
        }

        /// <summary>
        /// 显示警告框
        /// </summary>
        /// <param name="strMsg"></param>
        public static void Alert(string strMsg)
        {
            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(strMsg);
        }

        /// <summary>
        /// 设置当前文档的位置
        /// </summary>
        /// <param name="position"></param>
        public void SetDocLocation(Point position)
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
#if CAD_2008
            acDoc.Window.WindowState = System.Windows.Forms.FormWindowState.Normal;
            acDoc.Window.Location = position;
#elif CAD_2014
            acDoc.Window.WindowState = Window.State.Normal;
            acDoc.Window.SetLocation( position );
#endif
        }

        public void SetDocSize(Size size)
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
#if CAD_2008
            acDoc.Window.WindowState = System.Windows.Forms.FormWindowState.Normal;
            acDoc.Window.Size = size;
#elif CAD_2014
            acDoc.Window.WindowState = Window.State.Normal;
            acDoc.Window.SetSize(size);
#endif
        }

        public static void WriteToTxtFile(string str)
        {
            try
            {
                FileStream file = new FileStream("test.txt", FileMode.OpenOrCreate);
                file.Seek(0, SeekOrigin.End);
                byte[] bytes = Encoding.UTF8.GetBytes(str);
                file.Write(bytes, 0, bytes.Length);
                file.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        public void MiscFunc()
        {
            // 设置文档位置  
            //docCur.Window.Location = pt;

            // 最小化  
            //docCur.Window.WindowState = Autodesk.AutoCAD.Windows.Window.State.Minimized;

            //docCur.Window.WindowState = Autodesk.AutoCAD.Windows.Window.State.Maximized;
            // 最大化 
        }

        /// <summary>
        /// 创建面板
        /// </summary>
        /// <param name="name">面板名</param>
        /// <param name="title">面板标题</param>
        /// <param name="myCtrl">用户控件</param>
        /// <returns></returns>
        public static PaletteSet NewPalette(string name, string title, System.Windows.Forms.UserControl myCtrl)
        {
            PaletteSet ps = new Autodesk.AutoCAD.Windows.PaletteSet(title);
            ps.Add(name, myCtrl);
            return ps;
        }

        /// <summary>
        /// 得到程序集所在的文件夹路径
        /// </summary>
        /// <returns></returns>
        public static string GetAppPath()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            //当前执行程序集完整路径
            string File = Assembly.GetExecutingAssembly().Location;
            //执行程序集所在目录
            FileInfo assinfo = new FileInfo(File);
            return assinfo.DirectoryName;
        }
    }

    public class OrderTempItem
    {
        public OrderTempItem()
        {

        }

        public string PaperName { get; set; }
        public string PaperCopies { get; set; }
        public int PaperPower { get; set; }

        public static List<OrderTempItem> FillListByDataTable(System.Data.DataTable  dt)
        {
            List<OrderTempItem> lst_oti = new List<OrderTempItem>();
            foreach( DataRow dr in dt.Rows)
            {
                OrderTempItem oti = new OrderTempItem();
                oti.PaperName = dr["图纸名称"].ToString();
                oti.PaperCopies = dr["张数"].ToString();
                oti.PaperPower = Convert.ToInt32( dr["排序权值"]);

                lst_oti.Add(oti);
            }

            return lst_oti;
        }

        public static Dictionary<string, OrderTempItem> FillDictionaryByDataTable(System.Data.DataTable dt)
        {
            Dictionary< string, OrderTempItem > dic_oti = new Dictionary<string, OrderTempItem >();
            foreach (DataRow dr in dt.Rows)
            {
                OrderTempItem oti = new OrderTempItem();
                oti.PaperName = dr["图纸名称"].ToString();
                oti.PaperCopies = dr["张数"].ToString();
                oti.PaperPower = Convert.ToInt32(dr["排序权值"]);

                dic_oti.Add(oti.PaperName, oti);
            }

            return dic_oti;
        }
    }

    public class ModelConvertHelper<T> where T : new()  // 此处一定要加上new()
    {

        public static IList<T> ConvertToModel(System.Data.DataTable dt)
        {

            IList<T> ts = new List<T>();// 定义集合
            Type type = typeof(T); // 获得此模型的类型
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;
                    if (dt.Columns.Contains(tempName))
                    {
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }
    }
}
